<?php
require_once 'model.php';
require_once 'controller.php';
require_once 'melbourne.php';
require_once 'venue_model.php';
include 'offer_model.php';
/**
 * venue_controller Class
 * Controller for the Venue Table
 * 
 * @author Eduardo Nava
 *
 */
class venue_controller extends controller{
	public $venueModel;

	/**
	 * Constructor
	 * @return void
	 */
	function __construct(){
		$this->venueModel = new venue_model();
	}

	/**
	 * View a Venue action
	 * Retrieves all the information about a venue
	 * @return void
	 */
	function view(){
		if(isset($this->request['id'])){
			$venue = new venue_model();
			$offer=new offer_model();
			$this->request['venue_id']=$this->request['id'];
			$view->offers = $offer->find($this->request);
			$view->venue = $venue->get($this->request['id']);
			include('venue_view.php');
		}
	}

	//TODO refactor: almost same as find;
	/**
	 * Default action to perform when no action is called
	 * @param $args page
	 * @return void
	 */
	function index(){
		if(!isset($this->request['page'])){
			$this->request['page'] = 0;
		}
		$view->venues = $this->venueModel->getAll($this->request);
		$view->suburbs = melbourne::getSuburbs();
		$view->prevPageLink = $this->getPrevNextPage("prev");
		$view->nextPageLink = $this->getPrevNextPage("next");
		include('venue_index_view.php');
	}

	/**
	 * Search for a Venue action
	 * @param $args
	 * @return void
	 */
	function find(){
		if(!isset($this->request['page'])){
			$this->request['page'] = 0;
		}
		$view->venues = $this->venueModel->find($this->request);
		$view->suburbs = melbourne::getSuburbs();
		$view->prevPageLink = $this->getPrevNextPage("prev");
		$view->nextPageLink = $this->getPrevNextPage("next");
		include('venue_index_view.php');
	}

	/**
	 * Function to store a venue Rate
	 * @return
	 */
	function rate(){
		$this->requireLogin();
		$rate = new model("venue_rates");
		$this->request["user_id"]=$_SESSION['user']['id'];
		$this->request["venue_id"]=$this->request['id'];

		if($rate->create($this->request)){
			$this->setFlash("Thanks for rating!");
		}
		else {
			$this->setFlash("Upps! there was a problem with your rate, please try again.");
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
}
?>